package com.zhenyi.tuangou.admin.index;

import java.util.List;

import com.jfinal.kit.Kv;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.zhenyi.common.model.TgOrder;
import com.zhenyi.common.model.TgUser;
import com.zhenyi.tuangou.core.OrderUtils;

public class OrderController extends AdminController {

	@Override
	public void list() {
		Kv kv = Kv.by("shopId", getShopId());
		String orderNo = getPara("orderNo");
		if (StrKit.notBlank(orderNo)) {
			kv.set("orderNo", orderNo);
		}
		String startTime = getPara("startTime");
		String endTime = getPara("endTime");
		if (StrKit.notBlank(endTime, startTime)) {
			kv.set("startTime", startTime);
			kv.set("endTime", endTime);
		}
		Integer status = getParaToInt("status");
		if (status != null) {
			kv.set("status", status);
		}
		SqlPara sqlPara = Db.getSqlPara("FindAdminOrders", kv);
		Page<Record> pages = Db.paginate(getPage(), PAGE_SIZE, sqlPara);
		for (Record item : pages.getList()) {
			String extra = item.getStr("extra");
			if (extra != null) {
				if (extra.contains(OrderUtils.GROUP_PRODUCT) || extra.contains(OrderUtils.GROUP)) {
					item.set("isGroup", 1);
				} else {
					item.set("isGroup", 0);
				}
			}
			if (item.getStr("address") != null) {
				String[] info = item.getStr("address").split(" ");
				item.set("name", info[0]);
				item.set("mobile", info[1]);
				item.set("addr", info[2]);
			}
		}
		renderPage(pages);
	}

	@Override
	public void info() {
		Long id = getParaToLong("id");
//		TgOrder order = TgOrder.dao.findById(id);
//		renderSuccess(order);
		
		Kv cond = Kv.by("orderId", id);
		SqlPara sqlPara = Db.getSqlPara("adminOrderInfo", cond);
		List<Record> productList = null;
		Record record = Db.findFirst(sqlPara);
		if (record == null) {
			renderError(404);
			return;
		}
		if (record.getLong("productItemId") == 0 && record.getLong("groupProductId") != 0) {// 团购订单
			SqlPara sqlPara2 = Db.getSqlPara("adminTgOrderProductInfo", cond);
			productList = Db.find(sqlPara2);
			productList.get(0).set("size", productList.get(0).getInt("size") + "人团");
		} else {
			SqlPara sqlPara2 = Db.getSqlPara("adminOrderProductInfo", cond);
			productList = Db.find(sqlPara2);
		}
		record.set("productList", productList);
		
		Long userId = record.get("userId");
		record.set("userName", TgUser.dao.findById(userId).get("name"));
		
		renderSuccess(record);
	}

	/**
	 * 发货
	 */
	public void onShip() {
		Long id = getParaToLong("id");
		String trackingNo = getPara("trackingNo");
		if (id == null) {
			renderFailure(500, "id is null");
			return;
		}
		TgOrder order = TgOrder.dao.findById(id);
		if (order == null) {
			renderFailure(404, "order not found");
			return;
		}
		order.setTrackingNo(trackingNo);
		order.setIsShipped(1);
		boolean flag = order.update();
		if (flag) {
			renderSuccess();
		} else {
			renderFailure();
		}
	}

	/**
	 * 换退货
	 */
	public void close() {
		Long id = getParaToLong("id");
		if (id == null) {
			renderFailure(500, "id is null");
			return;
		}
		TgOrder order = TgOrder.dao.findById(id);
		if (order == null) {
			renderFailure(404, "order not found");
			return;
		}
		order.setIsClosed(1);
		boolean flag = order.update();
		if (flag) {
			renderSuccess();
		} else {
			renderFailure();
		}
	}
}
